package com.bsoft.gol.hcs.visitrecord.service.impl;

import com.bsoft.gol.hcs.enumcase.VisitRecordEnum;
import com.bsoft.gol.hcs.utils.CommonUtils;
import com.bsoft.gol.hcs.utils.DateUtil;
import com.bsoft.gol.hcs.visitrecord.bean.qo.HealthRecordParamDTO;
import com.bsoft.gol.hcs.visitrecord.bean.qo.HospitalVisitsRecordListQo;
import com.bsoft.gol.hcs.visitrecord.bean.vo.HealthRecordParamVO;
import com.bsoft.gol.hcs.visitrecord.bean.vo.HospitalVisitsRecordListVo;
import com.bsoft.gol.hcs.visitrecord.dao.*;
import com.bsoft.gol.hcs.visitrecord.entity.VrInpatientVisitRecord;
import com.bsoft.gol.hcs.visitrecord.service.HospitalVisitsRecordService;
import com.bsoft.gol.mpi.model.Certificate;
import com.bsoft.gol.mpi.model.Person;
import com.bsoft.gol.mpi.provider.IMPIProvider;
import com.bsoft.gol.mpi.service.MPIException;
import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevReference;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.base.VrOutpatientVisitRecord;
import hcn.bean.UserAllVo;
import hcn.service.base.IOrganizationService;
import lombok.extern.slf4j.Slf4j;
import org.atag.dao.model.SimplePageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import pcn.service.web.BaseDeviceService;

import java.util.Calendar;
import java.util.Objects;

/**
 * Created by ChenJij on 2020/9/6.
 */
@SsdevService("hospitalVisitsRecord")
@Slf4j
public class HospitalVisitsRecordServiceImpl implements HospitalVisitsRecordService {

    @Autowired
    private VrOutpatientVisitRecordDAO vrOutpatientVisitRecordDAO;
    @Autowired
    private VrInpatientVisitRecordDAO vrInpatientVisitRecordDAO;
    @Autowired
    private VrSurgicalRecordDAO vrSurgicalRecordDAO;
    @Autowired
    private VrApplicationFormRecordDAO vrApplicationFormRecordDAO;
    @Autowired
    private VrDispensingRecordDAO vrDispensingRecordDAO;
    @Value("${database_type}")
    private String databaseType;

    @SsdevReference("mpi.mpiProvider")
    private IMPIProvider mpiProvider;

    @SsdevReference("hcn.organization")
    private IOrganizationService organizationService;
    @SsdevReference("hcn.baseDeviceService")
    private BaseDeviceService baseDeviceService;


    @RpcService
    @Override
    public SimplePageInfo<HospitalVisitsRecordListVo> queryHospitalVisitsRecordList(HospitalVisitsRecordListQo qo) {
        if (CommonUtils.isNotEmpty(qo.getEndDate())) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(DateUtil.getDateTime("yyyy-MM-dd", qo.getEndDate()));
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            qo.setEndDate(DateUtil.formDate(calendar.getTime(), "yyyy-MM-dd"));
        }
        SimplePageInfo<HospitalVisitsRecordListVo> simplePageInfo = null;
        String groupConcat = "GROUP_CONCAT";
        String billCategoryCode;
        //1 mysql   2 达梦
        if ("2".equals(databaseType)) {
            groupConcat = "wm_concat";
        }
        String businessType = qo.getBusinessType();
        if (CommonUtils.isNotEmpty(businessType)) {
            VisitRecordEnum visitType = VisitRecordEnum.getEnumByKey(Byte.parseByte(businessType));
            switch (visitType) {
                //门诊记录
                case OUTPATIENT_VISIT_RECORD:
                    simplePageInfo = vrOutpatientVisitRecordDAO.queryOutpatientVisitRecordList(qo);
                    break;
                //住院记录
                case INPATIENT_VISIT_RECORD:
                    simplePageInfo = vrInpatientVisitRecordDAO.queryOutpatientVisitRecordList(qo);
                    break;
                //手术记录
                case SURGICAL_RECORD:
                    simplePageInfo = vrSurgicalRecordDAO.queryOutpatientVisitRecordList(qo);
                    break;
                //检验记录
                case INSPECTION_RECORD:
                    billCategoryCode = "1";
                    qo.setBillCategoryCode(billCategoryCode);
                    simplePageInfo = vrApplicationFormRecordDAO.queryOutpatientVisitRecordList(qo, groupConcat);
                    break;
                //检查记录
                case CHECK_RECORD:
                    billCategoryCode = "2";
                    qo.setBillCategoryCode(billCategoryCode);
                    simplePageInfo = vrApplicationFormRecordDAO.queryOutpatientVisitRecordList(qo, groupConcat);
                    break;
                //取药记录
                case DISPENSING_RECORD:
                    simplePageInfo = vrDispensingRecordDAO.queryOutpatientVisitRecordList(qo);
                    break;
                //治疗记录
                case TREATMENT_RECORD:
                    billCategoryCode = "3";
                    qo.setBillCategoryCode(billCategoryCode);
                    simplePageInfo = vrApplicationFormRecordDAO.queryOutpatientVisitRecordList(qo, groupConcat);
                    break;
                default:
                    simplePageInfo = new SimplePageInfo<>();
            }
        }
        return simplePageInfo;
    }

    @Override
    public HealthRecordParamVO getHealthRecordParam(HealthRecordParamDTO dto) {
        String visitNo = dto.getVisitNo();
        String visitType = dto.getVisitType();
        String organizationId = dto.getOrganizationId();
        VisitRecordEnum enumByKey = VisitRecordEnum.getEnumByKey(visitType);
        if (Objects.isNull(enumByKey)) {
            return null;
        }
        HealthRecordParamVO vo = new HealthRecordParamVO();

        switch (enumByKey) {
            case OUTPATIENT_VISIT_RECORD:
                VrOutpatientVisitRecord record = vrOutpatientVisitRecordDAO.getVrOutpatientVisitRecord(visitNo, organizationId);
                if (Objects.nonNull(record)) {
                    vo.setCardType(record.getCardType());
                    vo.setCardNumber(record.getCardNumber());
                    vo.setPatientId(record.getPatientId());
                    vo.setPatientName(record.getPatientName());
                    vo.setOrganizationId(record.getOrganizationId());
                    vo.setDocId("");
                }
                break;
            case INPATIENT_VISIT_RECORD:
                VrInpatientVisitRecord recordIn = vrInpatientVisitRecordDAO.getVrInpatientVisitRecord(visitNo, organizationId);
                if (Objects.nonNull(recordIn)) {
                    vo.setCardType(recordIn.getCardType());
                    vo.setCardNumber(recordIn.getCardNumber());
                    vo.setPatientId(recordIn.getPatientId());
                    vo.setPatientName(recordIn.getPatientName());
                    vo.setOrganizationId(recordIn.getOrganizationId());
                    vo.setDocId("");
                }
                break;
            default:
                break;
        }
        //查询机构
        if(!StringUtils.isEmpty(vo.getOrganizationId())){
            try {
                String orgId = organizationService.getOrgIdByOrgCode(vo.getOrganizationId());
                vo.setOrgId(orgId);
            } catch (ControllerException e) {
                log.error("获取机构信息失败");
            }
        }

        //查询person
        if (!StringUtils.isEmpty(vo.getCardNumber()) && !StringUtils.isEmpty(vo.getCardType())) {
            //查询personId
            Certificate cert = new Certificate(vo.getCardType(), vo.getCardNumber(), "01");
            try {
                Person person = mpiProvider.getPersonByCertificate(cert);
                if (Objects.nonNull(person)) {
                    vo.setPersonId(person.getMpiId());
                }
            } catch (MPIException e) {
                log.error("获取用户信息失败");
            }
        }
        UserAllVo userVo = baseDeviceService.getAllByOnline();
        if(Objects.nonNull(userVo)){
            vo.setDocId(userVo.getDoctorId());
        }
        return vo;
    }


}